<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_Theme_Menu
{
    protected $items = array();
    protected $keyname = '';
    protected $parentDepth;
    protected $title = 'Menu Item';
    protected $icon = '';
    protected $url = 'javascript: void(null)';
    protected $id = '';
    protected $class = '';

    public function set($name, $value){
        if($name === 'content'){
            return $this->setContent($value);
        }
        if(isset($this->$name)) $this->$name = $value;
    }

    public function get($name){
        if(isset($this->$name)) return $this->$name;
    }

    public function __construct($parentDepth, $keyname, $title, $url='javascript: void(null)', $icon=''){
        $this->keyname = $keyname;
        $this->parentDepth = $parentDepth;
        $this->title = $title;
        $this->url = $url;
        $this->icon = $icon;
    }

    public function __toString(){
        $THEME = JFX::registry('JFX_Theme');
        $content = $THEME->parseMenuTemplate($this);
        $items = '';
        foreach($this->items as $k=>$v){
            $items .= (string) $v;
        }
        $content = str_replace(':|items|:', $items, $content);
        return $content;
    }

    public function addItem($position, $keyname, $title, $url='javascript: void(null)', $icon=''){
        $position = (int) $position;
        if($position < 0) $position = 0;
        if($position > count($this->items)) $position = count($this->items);
        $obj = new JFX_Theme_Menu($this->parentDepth+1, $keyname, $title, $url, $icon);
        $this->items = array_insert($this->items, $position, $obj);
        return $obj;
    }

    public function remove($position){
        if(array_key_exists($position, $this->items)) unset($this->items[$position]);
        $this->items = array_regenerate_keys($this->items);
    }

    public function find($keyname){
        foreach($this->items as $k=>$v){
            if($v->get('keyname')==$keyname){
                return $k;
            }
        }
    }

    public function findRecursive($keyname){
        foreach($this->items as $k=>$v){
            if($v->get('keyname')==$keyname){
                return $v;
            }
        }
        // not found
        $found = false;
        foreach($this->items as $k=>$v){
            $found = $v->findRecursive($keyname);
            if(is_array($found)) return $found;
        }
        return false;
    }

    public function getItem($position){
        if(array_key_exists($position, $this->items)) return $this->items[$position];
    }

    public function getItems(){
        return $this->items;
    }

    public function move($existingPos, $newPos){
        if(!array_key_exists($existingPos, $this->items)) return false;
        $item = $this->items[$existingPos];
        unset($this->items[$existingPos]);
        $this->items = array_regenerate_keys($this->items);
        $this->items = array_insert($this->items, $newPos, $item);
        $this->items = array_regenerate_keys($this->items);
        return true;
    }

}